

#include "DisplayObject.h"


#include <fstream>
#include "FileHelpers.h"
#include <gl\glew.h>
#include <gl\glfw.h>
#include "shapes3d.h"

using std::ifstream;

void DisplayObject::render()
{
	glPushMatrix();
	//glRotatef(-90,1,0,0);
	glMultMatrixf( &_matrix[0] );
	
	

	glDisable(GL_LIGHTING);
	glColor3f(1,0,0);

	if( strcmp(_blenderType, "LAMP")==0 )
	{
		glColor3f(1,1,0);
		Shapes3D::DrawSphere(0.5f, 12, 12);
	}

	if( strcmp(_blenderType, "CAMERA")==0 )
	{
		glColor3f(0,0,1);
		Shapes3D::DrawCube(.5);
	}
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glPointSize(3.0);

	glBegin(GL_POINTS);
	for(size_t i = 0; i < verts.size() ; i++)
	{
		glVertex3f( verts[i].x(), verts[i].y(), verts[i].z() );
	}
	glEnd();

	glEnable(GL_LIGHTING);
	glColor3f(1,1,0);
	//glBegin(GL_TRIANGLES);
	//size_t nIdx = 0;
	//for(size_t i = 0; i < verts.size() ; i+=3)
	//{
	//	glNormal3f( normals[nIdx].x, normals[nIdx].y, normals[nIdx].z );
	//	glVertex3f( verts[i].x, verts[i].y, verts[i].z );
	//	glVertex3f( verts[i+1].x, verts[i+1].y, verts[i+1].z );
	//	glVertex3f( verts[i+2].x, verts[i+2].y, verts[i+2].z );
	//	nIdx++;
	//}
	//glEnd();

	if( verts.size() > 0 )
	{
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_NORMAL_ARRAY);
		//glEnableClientState(GL_COLOR_ARRAY);
		glVertexPointer(3, GL_FLOAT, 0, &(verts[0]).x());
		glNormalPointer(GL_FLOAT, 0, &(normals[0]).x());
		//glColorPointer(3, GL_FLOAT, 0, &colors[0]);

		glDrawArrays(GL_TRIANGLES, 0, verts.size() );

		// deactivate vertex arrays after drawing
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_NORMAL_ARRAY);
		//glDisableClientState(GL_COLOR_ARRAY);
	}



	glDisable(GL_LIGHTING);
	glColor3f(0,1,0);
	glBegin(GL_LINES);
	int nIdx = 0;
	float normalScale = 0.01f;
	for(size_t i = 0; i < verts.size() ; i+=3)
	{
		Vec3f centroid = verts[i] + verts[i+1] + verts[i+2];
		centroid /= 3.0f;
		glVertex3f( centroid.x(), centroid.y(), centroid.z() );
		glVertex3f( centroid.x() + normals[nIdx].x()*normalScale, centroid.y() + normals[nIdx].y()*normalScale, centroid.z() + normals[nIdx].z()*normalScale );
		nIdx++;
	}
	glEnd();

	glPopMatrix();
}

void DisplayObject::load( ifstream &inpFile )
{
	inpFile.read( (char*) &_id, sizeof(unsigned int) );
	_name = readAsciizString( inpFile );

	printf("Loading DisplayObj: \t%i %s \n", _id, _name);

	for (int i = 0; i < 4*4; i++)
		_matrix[i] = readFloat(inpFile);

	_blenderType = readAsciizString( inpFile );

	unsigned int hasMesh = readUint(inpFile);

	if(hasMesh)
	{
		unsigned int meshFaceCount = readUint(inpFile);
		loadColors( inpFile );

		if( meshFaceCount > 0)
		{
			float x,y,z;
			for(size_t i=0; i<meshFaceCount; i++)
			{
				if( i % 100 == 0 ) printf("\tloaded %i of %i faces\n", i, meshFaceCount);
				inpFile.read( reinterpret_cast<char *>( &x ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &y ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &z ), sizeof(float) );
				normals.push_back( Vec3f(x,y,z) );
				normals.push_back( Vec3f(x,y,z) );
				normals.push_back( Vec3f(x,y,z) );

				inpFile.read( reinterpret_cast<char *>( &x ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &y ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &z ), sizeof(float) );
				verts.push_back( Vec3f(x,y,z) );

				inpFile.read( reinterpret_cast<char *>( &x ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &y ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &z ), sizeof(float) );
				verts.push_back( Vec3f(x,y,z) );

				inpFile.read( reinterpret_cast<char *>( &x ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &y ), sizeof(float) );
				inpFile.read( reinterpret_cast<char *>( &z ), sizeof(float) );
				verts.push_back( Vec3f(x,y,z) );
			}
			printf("\tMesh %s faces loaded\n", _name);
		}else
		{
			printf("\tobject has no faces\n");
		}
	}


	
	
}

void DisplayObject::loadColors( ifstream &inpFile )
{
	unsigned int faceColorCount = readUint(inpFile);
	if( faceColorCount > 0 )
	{
		printf("\tLoading %i face colors\n", faceColorCount);
		//_colors.resize(faceColorCount);
		float r,g,b;
		for(size_t i=0; i<faceColorCount; i++){
			r = readFloat(inpFile);
			g = readFloat(inpFile);
			b = readFloat(inpFile);
			_colors.push_back( Vec3f(r,g,b) );
			_colors.push_back( Vec3f(r,g,b) );
			_colors.push_back( Vec3f(r,g,b) );
		}
	}
}

char* DisplayObject::getName()
{
	return _name;
}

Vec3f DisplayObject::getPos()
{
	Vec3f position( _matrix[12], _matrix[13], _matrix[14] );
	return position;
}
